//
//  QRTabbarView.m
//  QRMaster
//
//  Created by myApple on 15/12/17.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

enum
{
    KCTABBAR_QRM_MIANPAGE = 100000,
    KCTABBAR_QRM_GROUP,
    KCTABBAR_QRM_QUICK,
    KCTABBAR_QRM_FOUND,
    KCTABBAR_QRM_MINE,
};
#import "QRTabbarView.h"
#import "OpertionPlist.h"

@interface QRTabbarView()
{
    CGRect Frame;
    UIImageView * circleImage;//中间大圆btn
    NSArray * chineseArray;
    NSArray * englishArray;
    
    NSArray * titleArray;
    NSArray * imageArray;
    NSArray * highlightImgArray;
}

@end

@implementation QRTabbarView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        Frame = frame;
        self.backgroundColor = UIColorFromRGB(0xffffff);
        [self prepareTitle];
         [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLanguage) name:@"changeLanguage"object:nil];
//        [self prepareBtn];
    }
    return self;
}

-(void)prepareTitle
{
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *string = [def valueForKey:@"userLanguage"];
//    if (![string isEqualToString:@"zh-Hans"] && ![string isEqualToString:@"en"]) {
//       chineseArray = @[NSLocalizedString(@"Manage", nil),NSLocalizedString(@"Card", nil),@"11",NSLocalizedString(@"Find", nil),NSLocalizedString(@"Account", nil)];
//    }
//    else
//    {
//        chineseArray = @[@"管理",@"房卡",@"11",@"发现", @"用户"];
//    }
//    englishArray = @[@"Manage",@"Card",@"11",@"Find", @"Account"];
    titleArray = @[@"房卡",@"管理",@"消息",@"用户"];
    imageArray = @[@"roomcard_grey.png",@"administrate_grey.png",@"message_grey.png",@"user_grey.png"];
    highlightImgArray = @[@"roomcard_green.png",@"administrate_green.png",@"message_greeen.png",@"user_green.png"];
}

-(void)prepareBtn
{
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Frame.size.width, 0.5)];
    lineView.backgroundColor = RGBA(199, 201, 201, 2);
    [self addSubview:lineView];
    
//    for (int i = 0 ; i < TABBARCOUNT; i ++) {
//        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn.frame = CGRectMake(0+(Frame.size.width / TABBARCOUNT) * i,0, (Frame.size.width / TABBARCOUNT), Frame.size.height);
//        btn.tag = KCTABBAR_QRM_MIANPAGE + i;
//        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
//        [btn setTitle:chineseArray[i] forState:UIControlStateNormal];
//        [btn setTitleEdgeInsets:UIEdgeInsetsMake(8, 0, 22, 0)];
//        UILabel * englishLabel = [[UILabel alloc]initWithFrame:CGRectMake(btn.frame.origin.x, btn.frame.origin.y + 25, btn.frame.size.width, 20)];
//        englishLabel.font = [UIFont systemFontOfSize:12.0];
//        englishLabel.tag = 2000 + i;
//        englishLabel.text = englishArray[i];
//        englishLabel.textColor = RGBA(211, 211, 211, 1);
//        englishLabel.textAlignment = NSTextAlignmentCenter;
//        [self addSubview:englishLabel];
//        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:btn];
//    }
//    NSMutableDictionary * dict = [OpertionPlist readDataFromPlist];
//    NSMutableArray * cardInfoArray = [dict objectForKey:FAVERITCARD];
//    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
//    if ([[userInfo objectForKey:@"quickCard"]isEqualToString:@"1"]|| cardInfoArray.count > 0) {
//        
//        UIButton * noramlBtn = (UIButton *)[self viewWithTag:KCTABBAR_QRM_MIANPAGE + 2];
//        [self btnPressed:noramlBtn];
//    }
//    else
//    {
//        UIButton * noramlBtn = (UIButton *)[self viewWithTag:KCTABBAR_QRM_MIANPAGE + 0];
//        [self btnPressed:noramlBtn];
//    }
//    
//    circleImage = [[UIImageView alloc]initWithFrame:CGRectMake(Frame.size.width / 2.0 - 30, -15, 60, 60)];
//    circleImage.backgroundColor = RGBA(125, 194, 25, 1);
//    circleImage.layer.masksToBounds = YES;
//    circleImage.layer.cornerRadius = 30;
//    circleImage.userInteractionEnabled = NO;
//    circleImage.image = [UIImage imageNamed:@"ic_quick"];
//    [self addSubview:circleImage];
    
    for (int i=0; i<4; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0+(Frame.size.width / 4) * i,0, (Frame.size.width / 4), Frame.size.height);
        btn.tag = KCTABBAR_QRM_MIANPAGE + i;
        btn.backgroundColor = [UIColor clearColor];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((btn.frame.size.width-24)/2, btn.frame.size.height-3-10, 24, 10)];
        titleLabel.tag = 2000+i;
        titleLabel.font = [UIFont systemFontOfSize:10];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = titleArray[i];
        [btn addSubview:titleLabel];
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake((btn.frame.size.width-24)/2, titleLabel.frame.origin.y-5-24, 24, 24)];
        imageView.tag = 3000+i;
        imageView.image = [UIImage imageNamed:imageArray[i]];
        [btn addSubview:imageView];
        [self addSubview:btn];
    }
    UIButton * noramlBtn = (UIButton *)[self viewWithTag:KCTABBAR_QRM_MIANPAGE + 0];
    [self btnPressed:noramlBtn];
}


-(void)btnPressed:(UIButton *)button
{
    [self resetButtonImageWithPress:button];
    if (_delegate && [_delegate respondsToSelector:@selector(pressedCustomTabbar:)]) {
        [_delegate pressedCustomTabbar:button.tag - KCTABBAR_QRM_MIANPAGE];
    }
}

-(void)resetButtonImageWithPress:(UIButton *)button
{
//    for (int i = 0 ; i < TABBARCOUNT; i ++) {
//        UIButton * btn = (UIButton *)[self viewWithTag:i + KCTABBAR_QRM_MIANPAGE];
//        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        UILabel * lable = (UILabel *)[self viewWithTag:2000 + i];
//        lable.textColor = RGBA(211, 211, 211, 1);
//    }
//    
//    [button setTitleColor:RGBA(135, 199, 66, 1) forState:UIControlStateNormal];
//    UILabel * label = (UILabel *)[self viewWithTag:button.tag - KCTABBAR_QRM_MIANPAGE + 2000];
//    label.textColor = RGBA(135, 199, 66, 1);
//    if (button.tag == KCTABBAR_QRM_MIANPAGE + 2) {
//        [self animationCircleBtn];
//    }
    
    for (int i=0; i<4; i++) {
        UIButton * btn = (UIButton *)[self viewWithTag:i + KCTABBAR_QRM_MIANPAGE];
        UILabel * label = (UILabel *)[btn viewWithTag:i+2000];
        label.textColor = UIColorFromRGB(0x333333);
        UIImageView * imageView = (UIImageView *)[btn viewWithTag:i+3000];
        imageView.image = [UIImage imageNamed:imageArray[i]];
    }
    
    UILabel * label = (UILabel *)[button viewWithTag:button.tag-KCTABBAR_QRM_MIANPAGE+2000];
    label.textColor = UIColorFromRGB(0x7dc234);
    UIImageView * imageView = (UIImageView *)[button viewWithTag:button.tag-KCTABBAR_QRM_MIANPAGE+3000];
    imageView.image = [UIImage imageNamed:highlightImgArray[button.tag-KCTABBAR_QRM_MIANPAGE]];
}

-(void)changeLanguage
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *string = [def valueForKey:@"userLanguage"];
    if (![string isEqualToString:@"zh-Hans"] && ![string isEqualToString:@"en"]) {
        chineseArray = @[NSLocalizedString(@"Manage", nil),NSLocalizedString(@"Card", nil),@"11",NSLocalizedString(@"Find", nil),NSLocalizedString(@"Account", nil)];
    }
    else
    {
        chineseArray = @[@"管理",@"房卡",@"11",@"发现", @"用户"];
    }
    for (int i = 0 ; i < 5; i ++) {
        UIButton * selectBtn = (UIButton *)[self viewWithTag:KCTABBAR_QRM_MIANPAGE + i];
        [selectBtn setTitle:chineseArray[i] forState:0];
    }
}

-(void)animationCircleBtn
{
    [UIView animateWithDuration:0.4 animations:^{
        circleImage.transform = CGAffineTransformMakeRotation(M_PI_4);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.4 animations:^{
            circleImage.transform = CGAffineTransformMakeRotation(-M_PI_4);
        } completion:^(BOOL finished) {
            
        }];
    }];
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"changeLanguage" object:nil];
}

@end
